<template>
	<view class="content">
		<view class="navigationBox">
			<image class="returnImg" src="../../static/img/return.png" mode="" @tap="go('../index/home')"></image>
			<view class="title">
				学生管理
			</view>
			<image class="addImg" src="../../static/img/add.png" mode="" @tap="go('../student/addStudent')"></image>
			<image class="searchImg" src="../../static/img/search.png" mode="" @tap="searchShow()">
			</image>
		</view>
		<view class="searchBox" v-if="searchShowStatus == true">
			<view class="pickerBox">
				<view class="iptBox pickerItem">
					<input class="ipt" type="text" placeholder="输入关键词" placeholder-class="iptPla"
						v-model="info.search_data">
					<view class="searchBtn" @tap="getSearch()">
						搜索
					</view>
				</view>
			</view>
		</view>
		<v-tabs v-model="current" :tabs="tabs" @change="changeTab" bgColor="#F2F2F2" class="tabs"
			height="90rpx"></v-tabs>
		<view class="list">
			<view class="list-box" v-for="(item,index) in list">
				<view class="student-title-box">
					<image class="student-title-img" src="../../static/img/student/user.png"></image>
					<view class="username">{{item.username}}</view>
					<image class="student-title-img" src="../../static/img/student/phone.png"></image>
					<view class="username">{{item.phone}}</view>
					<image class="student-title-sex-img" src="../../static/img/student/sex.png"></image>
					<view class="username" v-if="item.sex == 1">男</view>
					<view class="username" v-if="item.sex == 2">女</view>
					<view class="username" v-if="item.sex == 3">未知</view>
				</view>
				<view class="student-info-box">
					<view class="student-info-left-box">
						<view class="student-info">学校：<text>{{item.school.school_name}}</text></view>
						<view class="level" v-if="item.level == 1">学生身份：<text>注册学生</text></view>
						<view class="level" v-if="item.level == 2">学生身份：<text>体验学生</text></view>
						<view class="level" v-if="item.level == 3">学生身份：<text>在读学生</text></view>
						<view class="level" v-if="item.level == 4">学生身份：<text>结业学生</text></view>
						<view class="student-info">注册时间：<text>{{item.create_time}}</text></view>
					</view>
					<view class="student-info-right-box">
						<view class="student-info">年级：<text>{{item.grade}}</text></view>
						<view class="vip-state">会员状态：<text>{{item.vip_state}}</text></view>
						<view class="student-info" v-if="item.from == 1">渠道：<text>个人</text></view>
						<view class="student-info" v-if="item.from == 2">渠道：<text>电销</text></view>
						<view class="student-info" v-if="item.from == 3">渠道：<text>抖音</text></view>
						<view class="student-info" v-if="item.from == 4">渠道：<text>地摊</text></view>
						<view class="student-info" v-if="item.from == 5">渠道：<text>合作</text></view>
						<view class="student-info" v-if="item.from == 6">渠道：<text>介绍</text></view>
					</view>
				</view>
				<view class="btn-box">
					<view class="check-btn" @tap="goStudentDetail(item.user_id)">查看</view>
					<view class="update-password-btn" @tap="updatePassword(item.user_id)">修改密码</view>
					<view class="update-btn" @tap="goEditStudent(item.user_id)">编辑</view>
					<view class="delete-btn" @tap="deleteStudent(item.user_id)">删除</view>
				</view>
			</view>
		</view>
		<!-- 修改密码弹框 -->
		<u-modal :show="passwordShow" @confirm="passwordConfirm" ref="uModal" :asyncClose="true"
			:showCancelButton="true" @cancel="passwordCancel">
			<view style="width: 100%;">
				<view class="item">
					<view class="label">
						密码
					</view>
					<view class="ipt">
						<input type="password" placeholder="请输入密码" v-model="new_password">
					</view>
				</view>
				<view class="item">
					<view class="label">
						确认密码
					</view>
					<view class="ipt">
						<input type="password" placeholder="请再次输入密码" v-model="confirm_new_password">
					</view>
				</view>
			</view>
		</u-modal>
		<!-- 删除弹框 -->
		<u-modal :show="deleteShow" @confirm="deleteConfirm" ref="uModal" :asyncClose="true" :showCancelButton="true"
			@cancel="deleteCancel">
			<view style="width: 100%;text-align: center;">
				确认删除该学生信息吗？
			</view>
		</u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				info: {
					search_data: '',
					type: 0,
					page: 1,
					size: 10
				},
				list: [],
				searchShowStatus: false,
				current: 0,
				tabs: ['全部', '在读学生', '注册学生', '体验学生', '结业学生'],
				pageSize: 10,
				pageTotal: 0,
				hasMore: true,
				passwordShow: false,
				new_password: '',
				confirm_new_password: '',
				password_user_id: 0,
				delete_user_id: 0,
				deleteShow: false
			};
		},
		onLoad() {
			this.getData()
		},
		onReachBottom() {
			this.getData();
		},
		methods: {
			searchShow() {
				if (this.searchShowStatus == false) {
					this.searchShowStatus = true;
				} else {
					this.searchShowStatus = false;
				}
			},
			changeTab(index) {
				this.info.type = index
				this.info.page = 1
				this.list = []
				this.getData();
			},
			getSearch() {
				this.info.page = 1
				this.list = []
				this.getData();
			},
			confirm(e) {
				this.info.start_date = e[0].replace(/-/g, "/");
				this.info.end_date = e[e.length - 1].replace(/-/g, "/");
				this.show = false
			},
			confirmHour(e) {
				this.hourText = e.value[0].label
				this.info.type = e.value[0].id
				this.hourShow = false
			},
			async getData() {
				uni.stopPullDownRefresh();
				const {
					data: res
				} = await uni.$http.post('school/student/getStudentList', this.info)
				if (res.code != 1) {
					return this.toast.msg(res.msg)
				}
				if (res.data.data.length) {
					this.list = this.list.concat(res.data.data);
					this.info.page += 1;
				} else {
					this.toast.msg('没有更多数据了！')
				}
			},
			updatePassword(user_id) {
				this.password_user_id = user_id
				this.passwordShow = true
			},
			passwordCancel() {
				this.passwordShow = false
				this.password_user_id = ''
				this.new_password = ''
				this.confirm_new_password = ''
			},
			deleteStudent(user_id) {
				this.delete_user_id = user_id
				this.deleteShow = true
			},
			async deleteConfirm() {
				const {
					data: res
				} = await uni.$http.post('school/student/delUser', {
					user_id: this.delete_user_id
				})
				if (res.code != 1) {
					this.deleteCancel()
					return this.toast.msg(res.msg)
				}
				this.toast.msg('删除成功')
				this.info.page = 1
				this.list = []
				this.getData();
				this.deleteCancel()
			},
			deleteCancel() {
				this.deleteShow = false
				this.delete_user_id = ''
			},
			async passwordConfirm() {
				if (this.new_password == '') {
					this.passwordCancel()
					return this.toast.msg('修改失败，未输入新密码')
				}
				if (this.confirm_new_password == '') {
					this.passwordCancel()
					return this.toast.msg('修改失败，未再次输入新密码')
				}
				if (this.new_password != this.confirm_new_password) {
					this.passwordCancel()
					return this.toast.msg('修改失败，两次输入的密码不一致')
				}

				const {
					data: res
				} = await uni.$http.post('school/student/savePassword', {
					user_id: this.password_user_id,
					new_password: this.new_password
				})
				if (res.code != 1) {
					this.passwordCancel()
					return this.toast.msg(res.msg)
				}
				this.toast.msg('修改成功')
				this.passwordCancel()
			},
			goEditStudent(user_id) {
				uni.navigateTo({
					url: `/pages/student/editStudent?user_id=${user_id}`
				})
			},
			goStudentDetail(user_id) {
				uni.navigateTo({
					url: `/pages/student/studentDetail?user_id=${user_id}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		.navigationBox {
			display: flex;
			align-items: center;
			height: 112rpx;
			padding: 50rpx 30rpx 0 51rpx;

			.returnImg {
				width: 52rpx;
				height: 52rpx;
			}

			.title {
				flex: 1;
				text-align: center;
			}

			.addImg {
				width: 23rpx;
				height: 23rpx;
			}

			.searchImg {
				width: 30rpx;
				height: 30rpx;
				margin-left: 30rpx;
			}
		}

		.searchBox {
			width: 670rpx;
			margin: 0 auto;
			padding: 0 20rpx;

			.pickerBox {
				font-size: 28rpx;
				height: 66rpx;
				line-height: 66rpx;
				display: flex;
				width: 100%;

				.pickerItem {
					display: flex;
					width: 100%;

					.frame {
						position: relative;
						width: 192rpx;
						text-align: center;
						color: #666666;
						border: 2rpx solid #FCF7F7;

						.lower {
							width: 42rpx;
							height: 42rpx;
							position: absolute;
							top: 50%;
							transform: translateY(-50%);
							right: 10rpx;
						}
					}
				}
			}

			.iptBox {
				display: flex;

				.ipt {
					box-sizing: border-box;
					width: 75%;
					height: 66rpx;
					padding-left: 75rpx;
					border: 2rpx solid #FCF7F7;
					border-radius: 10rpx;
				}

				.iptPla {
					height: 66rpx;
					line-height: 66rpx;
					color: #999;
					font-size: 22rpx;
				}

				.searchBtn {
					display: inline-block;
					background: #A6A6A6;
					width: 25%;
					height: 66rpx;
					line-height: 66rpx;
					text-align: center;
					border-radius: 10rpx;
					margin-left: 20rpx;
					font-size: 24rpx;
				}
			}
		}

		.list {
			width: 90%;
			margin-left: 5%;
			margin-top: 20rpx;
		}

		.list-box {
			width: 100%;
			height: 350rpx;
			border-radius: 5px;
			background: #F5F5F5;
			margin-bottom: 30rpx;
			padding: 20rpx 5%;
		}

		.student-title-box {
			width: 100%;
			height: 50rpx;
		}

		.student-title-img {
			width: 32rpx;
			height: 32rpx;
			margin-top: 9rpx;
			float: left;
		}

		.student-title-sex-img {
			width: 32rpx;
			height: 32rpx;
			margin-top: 12rpx;
			float: left;
		}

		.username {
			float: left;
			margin-left: 16rpx;
			margin-right: 40rpx;
			font-size: 26rpx;
			font-weight: 400;
			color: #1F2329;
			line-height: 50rpx;
		}

		.student-info-box {
			width: 100%;
			height: 140rpx;
			border-bottom: 1px solid #D0D3D5;
			margin-top: 40rpx;
			padding: 0 3%;
		}

		.student-info-left-box {
			width: 65%;
			height: 140rpx;
			float: left;
		}

		.student-info-right-box {
			width: 35%;
			height: 140rpx;
			float: left;
		}

		.student-info {
			font-size: 22rpx;
			font-weight: 400;
			color: #000000;
			line-height: 40rpx;
		}

		.student-info text {
			font-size: 22rpx;
			font-weight: 400;
			color: #666666;
			line-height: 40rpx;
		}

		.level {
			font-size: 22rpx;
			font-weight: 400;
			color: #000000;
			line-height: 40rpx;
		}

		.level text {
			font-size: 22rpx;
			font-weight: 400;
			color: #2A82E4;
			line-height: 40rpx;
		}

		.vip-state {
			font-size: 22rpx;
			font-weight: 400;
			color: #000000;
			line-height: 40rpx;
		}

		.vip-state text {
			font-size: 22rpx;
			font-weight: 400;
			color: #00BAAD;
			line-height: 40rpx;
		}

		.btn-box {
			width: 80%;
			margin-left: 20%;
			height: 46rpx;
			margin-top: 20rpx;
		}

		.check-btn {
			float: left;
			width: 20%;
			height: 46rpx;
			background: #808080;
			font-size: 12px;
			font-weight: 400;
			color: #FFFFFF;
			text-align: center;
			line-height: 46rpx;
		}

		.update-password-btn {
			float: left;
			width: 30%;
			height: 46rpx;
			background: #ff8d1a;
			font-size: 12px;
			font-weight: 400;
			color: #FFFFFF;
			text-align: center;
			line-height: 46rpx;
			margin-left: 3%;
		}

		.update-btn {
			float: left;
			width: 20%;
			height: 46rpx;
			background: #4E88E6;
			font-size: 12px;
			font-weight: 400;
			color: #FFFFFF;
			text-align: center;
			line-height: 46rpx;
			margin-left: 3%;
		}

		.delete-btn {
			float: left;
			width: 20%;
			height: 46rpx;
			background: #D43030;
			font-size: 12px;
			font-weight: 400;
			color: #FFFFFF;
			text-align: center;
			line-height: 46rpx;
			margin-left: 3%;
		}

		.tabs {
			margin-top: 20rpx;
		}

		.item {
			display: flex;
			align-items: center;
			padding: 0 35rpx 0 19rpx;
			width: 100%;
			height: 78rpx;
			line-height: 78rpx;
			border-radius: 10rpx;
			border: 2rpx solid #FCF7F7;
			margin-top: 20rpx;

			.label {
				display: flex;
				align-items: center;
				font-size: 28rpx;
				margin-right: 70rpx;
				width: 140rpx;
			}

			.ipt {
				flex: 1;
				position: relative;
				display: flex;
				align-items: center;
				color: #666;
			}
		}
	}
</style>